package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.common.yun.enums.DataUpStateEnum;
import com.lizhi.yun.db.stand.dao.StandPrescriptionActionDao;
import com.lizhi.yun.db.stand.dao.StandPrescriptionInfoDao;
import com.lizhi.yun.db.stand.pojo.StandPrescriptionActionPOJO;
import com.lizhi.yun.db.stand.pojo.StandPrescriptionInfoPOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/7/13 13:39
 */
@Slf4j
@Service
public class RecvPrescriptionInfoHandler {

    @Resource
    private StandPrescriptionInfoDao standPrescriptionInfoDao;

    @Resource
    private StandPrescriptionActionDao standPrescriptionActionDao;

    /**
     * 接收处方的同步信息
     * @param json
     * @return
     */
    public String recvPrescriptionInfo(String json){
        UpDataPOJO upDataPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upDataPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*处方信息*/
        StandPrescriptionInfoPOJO standPrescriptionInfoPOJO = SamJsonUtil.toObject(upDataPOJO.getData(), StandPrescriptionInfoPOJO.class);
        if (StringUtils.isEmpty(standPrescriptionInfoPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        standPrescriptionInfoPOJO.setHospitalId(rpcClientPOJO.getOrganId());
        standPrescriptionInfoPOJO.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*数据上传的时间*/
        standPrescriptionInfoPOJO.setPreUpState(DataUpStateEnum.DATA_UP_STATE_01.getEnValue());
        standPrescriptionInfoPOJO.setPreUpTm(DateUtils.getNowDate());
        log.info("上传的数据信息是standPrescriptionInfoPOJO:{}", standPrescriptionInfoPOJO);

        StandPrescriptionInfoPOJO prescriptionInfoPOJO = standPrescriptionInfoDao.selectById(standPrescriptionInfoPOJO.getPreId(), standPrescriptionInfoPOJO.getHospitalId(), standPrescriptionInfoPOJO.getHospitalSystemId());
        if (StringUtils.isEmpty(prescriptionInfoPOJO)){
            standPrescriptionInfoPOJO.setLid(StringUtils.newId());
            standPrescriptionInfoDao.insertRecord(standPrescriptionInfoPOJO);
        } else {
            standPrescriptionInfoDao.updateByIdSelective(standPrescriptionInfoPOJO);
        }

        /*先删除于此相关的处方记录*/
        standPrescriptionActionDao.deleteListById(standPrescriptionInfoPOJO.getPreId(), standPrescriptionInfoPOJO.getHospitalId(), standPrescriptionInfoPOJO.getHospitalSystemId());
        /*处方记录信息*/
        List<StandPrescriptionActionPOJO> standPrescriptionActionPOJOS = standPrescriptionInfoPOJO.getStandPrescriptionActionPOJOS();
        for (int i = 0; i < standPrescriptionActionPOJOS.size(); i++){
            StandPrescriptionActionPOJO standPrescriptionActionPOJO = standPrescriptionActionPOJOS.get(i);
            /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
            standPrescriptionActionPOJO.setHospitalId(rpcClientPOJO.getOrganId());
            standPrescriptionActionPOJO.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
            /*数据上传的时间*/
            standPrescriptionActionPOJO.setPreActionUpState(DataUpStateEnum.DATA_UP_STATE_01.getEnValue());
            standPrescriptionActionPOJO.setPreActionUpTm(DateUtils.getNowDate());
            standPrescriptionActionPOJO.setLid(StringUtils.newId());
            standPrescriptionActionDao.insertRecord(standPrescriptionActionPOJO);
        }
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
